package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.MyCarInfoEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.swgk.core.util.UserOperating;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.net.util.RequestUtil;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;

public class SelfCarManagerViewModel extends BaseViewModel {
    public MutableLiveData<ListResponseV1<List<MyCarInfoEntity>>> ldList = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldCheckOrder = new MutableLiveData<>();
    private int mPage = 1;
    //当前认证状态:0 审核中 1 审核通过 2 审核不通过 3 已下架
    public int mState = -1;

    public SelfCarManagerViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取车辆列表
     */
    public void getCarList(Context context, boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        }
        RequestUtil requestUtil = RequestUtil.map();
        if (mState >= 0) {
            //认证状态:0 审核中 1 审核通过 2 审核不通过 3 已下架
            requestUtil.add("authenState", mState);
        }
        if (UserOperating.getInstance().isCompany()) {
            requestUtil.add("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        }
        if (UserOperating.getInstance().isTeam()) {
            requestUtil.add("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        }
        //车队类型 1 自营 2 挂靠
        requestUtil.add("type", 1);
        requestUtil.add("limit", 15);
        requestUtil.add("page", mPage);
        //
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().getGroupCarList(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity<ListResponseV1<List<MyCarInfoEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponseV1<List<MyCarInfoEntity>>> data) {
                        if (!data.isSuccess()) {
                            ldList.postValue(new ListResponseV1<>(false));
                            return;
                        }
                        ldList.postValue(data.getData());
                        if (data.getData().haveMoreData()) {
                            mPage = data.getData().nextPage();
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponseV1<List<MyCarInfoEntity>>>> call, Throwable t) {
                        super.onFailure(call, t);
                        ldList.postValue(new ListResponseV1<>(false));
                    }
                });
    }

    /**
     * 检查是否有绑定订单
     */
    public void checkOrder(Context context, String driverNo, String carNo) {
        RequestUtil requestUtil = RequestUtil.map();
        requestUtil.add("no", driverNo);
        requestUtil.add("carNo", carNo);
        requestUtil.add("isLooseDriver", 2);
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().checkIsHaveOrders(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        ldCheckOrder.postValue(data.getData());
                    }
                });
    }

    /**
     * 解绑司机
     */
    public void unbindDriver(Context context, String no) {
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().unbindDriver(no)
                .enqueue(new ICallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity data) {
                        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_UNBIND_DRIVER));
                    }
                });
    }
}
